package online.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import online.entity.UserDao;
import online.exception.RRException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Auther: 狼芒
 * @Date: 2021/8/24 0:02
 * @Descrintion: 测试控制器
 * @version: 1.0
 */
@Slf4j
@RestController
public class TestController {

    @ApiOperation("获取用户信息")
    @GetMapping("/user/{name}/{age}")
    public Object user(@ApiParam("用户名") @PathVariable("name") String name,
                       //获取单个请求参数
                       @PathVariable("age") Integer age,
                       //将请求参数封装成Map进行传递
                       @PathVariable Map params,
                       //获取单个请求头
                       @RequestHeader("User-Agent") String userAgent,
                       //将请求头封装成Map进行传递
                       @RequestHeader Map hander,
                       //获取单个cookie的值
                       @CookieValue("_ga") String ga
    ) {
        UserDao userDao = new UserDao();
        userDao.setName(name);
        userDao.setAge(age);
        params.put("user", userDao);
        params.put("userAgent", userAgent);
        params.put("handers", hander);
        params.put("ga", ga);
        return params;
    }

    @PostMapping("post")
    public Object postMethod(@RequestBody Map map) {
        return map;
    }

    @RequestMapping("/zero")
    public String zero() {
        int i = 10 / 0;
        return "zero" + i;
    }


    @RequestMapping("/RRException")
    public String RRException() {
        if (true) {
            throw new RRException();
        }
        return "zero";
    }

    @RequestMapping("/RRException1/{a}")
    public String RRException1(@PathVariable("a") Integer a) {
        return "RRException1" + a;
    }

    @Value("${environment: unKnow}")
    String environment;

    @RequestMapping("/environment")
    public String environment() {
        return "environment: " + environment;
    }

    @GetMapping("/log")
    public String log() {
        log.trace("trace 信息");
        log.info("info 信息");
        log.debug("debug 信息");
        log.warn("warn 信息");
        log.error("error 信息");
        return environment;
    }
}
